﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitView;

namespace YJKRevitAxisModule
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdGridsMerge : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)15;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Transaction transaction = new Transaction(cmdData.Application.ActiveUIDocument.Document, "轴线合并");
			GridsMerge gridsMerge = new GridsMerge(cmdData);
			for (;;)
			{
				transaction.Start();
				Result result = gridsMerge.Run(ref message);
				if (result != Autodesk.Revit.UI.Result.Succeeded)
				{
					break;
				}
				transaction.Commit();
				if (result != Autodesk.Revit.UI.Result.Succeeded)
				{
					return Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			transaction.RollBack();
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
